Title: A Journey Through Universal Health Coverage for RMNCH Interventions.

Author: Pratapsingh Patil

Date: 27 April 2025.

Code
import plotly.io as pio
import polars as pl
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
import pandas as pd
import numpy as np
import statsmodels.api as sm  # For LOWESS
import ipywidgets as widgets
from IPython.display import display

# Load CSV files with Polars
healthcare_df = pl.read_csv('Healthcare.csv')
economic_df = pl.read_csv(
    'unicef_metadata.csv',
    schema_overrides={'Population, total': pl.Float64},
    null_values=['#DIV/0!']
)

# Data cleaning and preparation
healthcare_df = healthcare_df.with_columns([
    pl.col('year').cast(pl.Int32),
    pl.col('obs_value').cast(pl.Float64),
    pl.col('Country').cast(pl.Utf8)
])

economic_df = economic_df.with_columns([
    pl.col('year').cast(pl.Int32),
    pl.col('GDP per capita (constant 2015 US$)').cast(pl.Float64),
    pl.col('Life expectancy at birth, total (years)').cast(pl.Float64),
    pl.col('Country').cast(pl.Utf8),
    pl.col('Continent').cast(pl.Utf8)
])

merged_df = healthcare_df.join(economic_df, on=['Country', 'year'], how='inner')

# Convert Polars to Pandas
merged_pd = merged_df.to_pandas()

# Rename for clarity
merged_pd = merged_pd.rename(columns={'obs_value': 'Healthcare_Access_Percentage'})

# Prepare the latest year data
latest_year = merged_pd['year'].max()
df_latest = merged_pd[merged_pd['year'] == latest_year]

# Create the line chart function
def plot_line_chart(country):
    country_data = merged_pd[merged_pd['Country'] == country]
    fig = px.line(
        country_data,
        x='year',
        y='Healthcare_Access_Percentage',
        title=f"Healthcare Access Over Years: {country}",
        markers=True,
        labels={'Healthcare_Access_Percentage': '% Access to Healthcare'}
    )
    return fig

# Create the dropdown widget
country_dropdown = widgets.Dropdown(
    options=df_latest['Country'].unique(),
    description='Country:',
    value=df_latest['Country'].iloc[0],  # Default value
    style={'description_width': 'initial'}
)

# Update the line chart based on dropdown selection
def update_chart(change):
    if change['type'] == 'change' and change['name'] == 'value':
        fig = plot_line_chart(change['new'])
        fig.show()

# Attach the update function to the dropdown
country_dropdown.observe(update_chart)

# Display the dropdown and initial chart
display(country_dropdown)

# Display the initial chart for the default country
initial_fig = plot_line_chart(country_dropdown.value)
initial_fig.show()

# Save everything into one HTML file using Plotly's `write_html`
final_fig = plot_line_chart(country_dropdown.value)
final_fig.write_html("interactive_healthcare_access.html")
Code
# 4. Map
# Convert Polars to Pandas
merged_pd = merged_df.to_pandas()

# Rename for clarity
merged_pd = merged_pd.rename(columns={'obs_value': 'Healthcare_Access_Percentage'})

# Prepare the latest year data
latest_year = merged_pd['year'].max()
df_latest = merged_pd[merged_pd['year'] == latest_year]

# Plot the world map (static)
map_fig = px.choropleth(
    df_latest,
    locations="Country",
    locationmode="country names",
    color="Healthcare_Access_Percentage",
    hover_name="Country",
    color_continuous_scale="Blues",
    labels={'Healthcare_Access_Percentage': '% RMNCH Healthcare Access'},
    title="World Map: Women's Healthcare Access (Latest Year)"
)

# Show map
map_fig.show()
map_fig.write_html("interactive_world_map.html")
Code
# 3. Line plot
plt.figure(figsize=(12, 6))
sns.lineplot(data=melted_df, x='year', y='Value', hue='Indicator', marker='o', errorbar=None)

# Annotate with values
agg_df = melted_df.groupby(['year', 'Indicator'])['Value'].mean().reset_index()
for _, row in agg_df.iterrows():
    plt.text(
        x=row['year'],
        y=row['Value'],
        s=f"{row['Value']:.1f}",
        fontsize=8,
        ha='right' if row['Indicator'] == 'UHC Index (Reproductive, Maternal, Newborn, and Child-Health Interventions)' else 'left',
        va='bottom',
        bbox=dict(facecolor='white', alpha=0.5, edgecolor='none', boxstyle="round,pad=0.2")
    )

plt.title('Change in UHC Access and Life Expectancy Over Time')
plt.xlabel('Year')
plt.ylabel('Value')
plt.legend(title='Indicator')
plt.grid(True, linestyle='--', alpha=0.6)  # Adding gridlines for better visibility
plt.tight_layout()

# Save the plot as a PNG file
plt.savefig('uhc_life_expectancy_over_time.png', dpi=300)

# Show the plot
plt.show()

Code
# 2. Scatter Plot: Average GDP per Capita vs Average UHC Index

# Compute average UHC and GDP per capita per country
avg_uhc_df = healthcare_df.group_by('Country').agg(
    pl.col('obs_value').mean().alias('Average UHC')
)

avg_gdp_df = economic_df.group_by('Country').agg(
    pl.col('GDP per capita (constant 2015 US$)').mean().alias('Average GDP per capita')
)

# Merge the two average datasets
avg_merged_df = avg_uhc_df.join(avg_gdp_df, on='Country', how='inner')

# Convert to pandas for Plotly and statsmodels
avg_merged_pd = avg_merged_df.to_pandas()

# Merge the 'Continent' information from the economic_df using Polars' unique() and converting to Pandas
economic_continent_df = economic_df.select(['Country', 'Continent']).unique().to_pandas()
avg_merged_pd = avg_merged_pd.merge(economic_continent_df, on='Country', how='left')

# Scatter plot
fig = px.scatter(
    avg_merged_pd,
    x='Average UHC',
    y='Average GDP per capita',
    hover_name='Country',
    color='Continent',  # Color by continent
    labels={
        'Average GDP per capita': 'Average GDP per Capita (USD)',
        'Average UHC': 'Average UHC Index'
    },
    title="Relationship between Average GDP per Capita and Average UHC Index",
    template="plotly_white"
)

# Fit LOWESS curve
lowess_result = sm.nonparametric.lowess(
    endog=avg_merged_pd['Average UHC'],
    exog=avg_merged_pd['Average GDP per capita'],
    frac=0.9
)

# Add LOWESS curve to the plot
fig.add_scatter(
    x=lowess_result[:, 1],
    y=lowess_result[:, 0],
    mode='lines',
    line=dict(color='red', width=3),
    name='Trendline'
)

fig.show()
Code
# 1. Bar Chart: Women with Healthcare Access by Country (Latest Year)
latest_year = merged_df['year'].max()
earliest_year = merged_df['year'].min()

# Filter data for latest and earliest years
latest_data = merged_df.filter(pl.col('year') == latest_year)
earliest_data = merged_df.filter(pl.col('year') == earliest_year)

# Join latest and earliest data by country
compare_df = latest_data.join(
    earliest_data.select(['Country', 'obs_value']).rename({'obs_value': 'obs_value_earliest'}),
    on='Country', how='inner'
)

# Calculate improvement, top 10 countries
compare_df = compare_df.with_columns(
    (pl.col('obs_value') - pl.col('obs_value_earliest')).alias('Improvement')
)

top10_improvements = compare_df.sort('Improvement', descending=True).head(10)

top10_pd = top10_improvements.to_pandas()
year_range = f'{earliest_year} to {latest_year}'

# Plotting bar chart
plt.figure(figsize=(12, 6))
sns.barplot(data=top10_pd, x='Country', y='Improvement', hue='Continent')
plt.title(f'Top 10 Countries with Biggest Improvement in Women\'s Healthcare Access ({year_range})')
plt.xlabel('Country')
plt.ylabel('Improvement (%)')
plt.xticks(rotation=45, ha='right')
plt.grid(True, axis='y')

# Legend
plt.legend(title='Continent', loc='upper left', bbox_to_anchor=(1, 1))
plt.show()